package managers;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedList;

import entities.Pracownik;
import entities.Probka;
import entities.TypBadan;
import entities.TypProbki;
import dataBase.DataBaseController;
import dataProviders.*;

public class RegistrationManager 
{	
	public static Probka tmpProbka = new Probka();
	public static LinkedList<TypBadan> tmpWybraneBadania;
	public static LinkedList<Integer> tmpPriorytetyDlaBadan = new LinkedList<Integer>();
	
	public static Object[] getListOfAllContractors(boolean czyOsoba)
	{
		return ContractorsDataProvider.getListOfAllContractors(czyOsoba).toArray();
	}
	public static boolean czyMoznaOdblokowacPrzyciskDodajZlecenie()
	{
		return (tmpProbka.getKontrahent() != null && 
				tmpProbka.getPacjent() != null &&
				tmpProbka.getTypProbki() != null &&
				tmpWybraneBadania.size() != 0 );
	}
	public static Object[] getListOfAllChosenExaminations()
	{
		return tmpWybraneBadania.toArray();		
	}
	public static Object[] getListOfSamplesTypes()
	{
		return SamplesDataProvider.getListOfSamplesTypes().toArray();
	}
	public static Object[] getListOfExaminationsForSampleType(TypProbki typProbki)
	{
		return SamplesDataProvider.getListaBadanDlaTypu(typProbki).toArray();
	}	
	public static Object[] getListOfAllPatients()
	{
		return PatientsDataProvider.getListOfAllPatients().toArray();		
	}
	public static boolean addNewOrder()
	{
		int idNowejProbki =  SamplesDataProvider.addSample(tmpProbka);
		tmpProbka.setId(idNowejProbki);
		Iterator<TypBadan> iterator= tmpWybraneBadania.iterator();
		Pracownik zalogowanyPracownik = UsersDataProvider.getLoggedUser();
		//System.out.println("prio"+tmpPriorytetyDlaBadan.getFirst());
		//System.out.println("idZal"+zalogowanyPracownik.getId());
		//System.out.println("ProbkaID"+tmpProbka.getId());
		try 
		{
			while(iterator.hasNext())
			{

						//System.out.println(iterator..getId());
				
				String sql = "INSERT INTO Badania (priorytet,rejestratorID, probkaID, typBadanID, wyniki, dataWykonania, uwagi, dataWeryfikacji, dataOdbioru, magisterID, technikID) " + "	VALUES ("+tmpPriorytetyDlaBadan.getFirst()+","+zalogowanyPracownik.getId()+","+ tmpProbka.getId() +","+ iterator.next().getId() +",null,null,null,null,null,null,null)";
				DataBaseController.db.dbSqlUpdate(sql);
			}
			DataBaseController.db.dbCommit();
			return true;
		}
		catch (SQLException e) 
		{
			e.printStackTrace();
			try {
				DataBaseController.db.dbRollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			return false;
		}

	}
	
	
}
